package com.lenovo.testreport.gray.web.languageTest.service;

import com.lenovo.testreport.general.service.GeneralReportService;
import com.lenovo.testreport.gray.web.languageTest.mapper.GrayWebLanguageEnglishPropertiesReportMapper;
import com.lenovo.testreport.gray.web.languageTest.model.GrayWebLanguageEnglishPropertiesReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author CJF
 * @date 2018/12
 * @version Updated at 2019/05/24 By Howie Lu
 */
@Service("GrayWebLanguageEnglishPropertiesReportService")
public class GrayWebLanguageEnglishPropertiesReportServiceImpl implements GeneralReportService {
    @Autowired
    private GrayWebLanguageEnglishPropertiesReportMapper mapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查找所有报告
     *
     * @param tablename
     * @return
     */
    @Override
    synchronized public List<GrayWebLanguageEnglishPropertiesReport> findAllReport(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/AllReport/";
        redisTemplate.setKeySerializer(redisSerializer);
        List<GrayWebLanguageEnglishPropertiesReport> List = (List<GrayWebLanguageEnglishPropertiesReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllReport(tablename);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //   System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    /**
     * 按类型查找报告
     *
     * @param tablename
     * @param type
     * @return
     */
    @Override
    synchronized public List<GrayWebLanguageEnglishPropertiesReport> findByType(String tablename, String type) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/ReportByType/" + type;
        redisTemplate.setKeySerializer(redisSerializer);
        List<GrayWebLanguageEnglishPropertiesReport> List = (List<GrayWebLanguageEnglishPropertiesReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectByType(tablename, type);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //System.out.println(key + " 缓存中没找到，访问数据库！");
        } //else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    /**
     * 按时间，版本查询报告
     *
     * @param tablename
     * @param start
     * @param end
     * @param version
     * @return
     */
    @Override
    synchronized public List<GrayWebLanguageEnglishPropertiesReport> findReportByTime(String tablename, String start, String end, String version) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/From" + start + "to" + end + "/Version:" + version;
        redisTemplate.setKeySerializer(redisSerializer);
        List<GrayWebLanguageEnglishPropertiesReport> List = (List<GrayWebLanguageEnglishPropertiesReport>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllReportByTime(tablename, start, end, version);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            //  System.out.println(key + " 缓存中没找到，访问数据库！");
        }// else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;

    }

    /**
     * 加载所有版本号
     *
     * @param tablename
     * @return
     */
    @Override
    public List<String> findAllVersion(String tablename) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = tablename + "/AllVersion";
        redisTemplate.setKeySerializer(redisSerializer);
        List<String> List = (List<String>) redisTemplate.opsForValue().get(key);
        if (List == null) {
            List = mapper.selectAllVersion(tablename);
            redisTemplate.opsForValue().set(key, List, 120, TimeUnit.SECONDS);
            // System.out.println(key + " 缓存中没找到，访问数据库！");
        }
        // else System.out.println(key + " 缓存中找到了，无需访问数据库！");
        return List;
    }
}
